/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package communicator;

import debug.DebugFrame;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mr.Black
 */
public class Receiver extends Thread {

    private final static int PORT = 9999;
    private ServerSocket serverSocket;
    private Socket clientSocket;
    public static Vector listClientOOS, listHostAddress;
    

    public Receiver() {
        try {
            serverSocket = new ServerSocket(PORT);
            DebugFrame.receivMsg("/*\t\t\t\t\t\t*/Receiver is running..." + "\n");
            listClientOOS = new Vector();
            listHostAddress = new Vector();
        } catch (IOException ex) {
            Logger.getLogger(Receiver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void run() {
        while (true) {
            try {                
                clientSocket = serverSocket.accept();
                
                if (Sender.checkHost2Connect(clientSocket.getInetAddress().getHostAddress()) == -1) {
                    listClientOOS.add(new ObjectOutputStream(clientSocket.getOutputStream()));
                    listHostAddress.add(clientSocket.getInetAddress().getHostAddress());
                    printClientInfo(clientSocket);
                    ProcessClient processClient = new ProcessClient(clientSocket);
                    processClient.start();
                }               
                
            } catch (IOException ex) {
                Logger.getLogger(Receiver.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void printClientInfo(Socket client) {
        DebugFrame.receivMsg("/*\t\t\t\t\t\t*/Vua co client moi ket noi toi: " + "\n");
        DebugFrame.receivMsg("/*\t\t\t\t\t\t*/\tIP   : " + client.getInetAddress().getHostAddress() + "\n");
        DebugFrame.receivMsg("/*\t\t\t\t\t\t*/\tPort : " + client.getLocalPort() + "\n");
    }

    
}
